﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelReport
{
    public class ExcelContent
    {

        //public List<ExcelCell> CellsItems { get; set; } = new List<ExcelCell>();
        public ExcelCell Cell { get; set; }

        //    public void WriteTo()
        //    {
        //        if(IsOpen)
        //        {
        //            if (workBook.Sheets.ContainSheet(SheetName))
        //            {
        //                Excel.Application excelApp = new Excel.Application();
        //                object missing = Missing.Value;
        //                object readOnly = true;
        //                Excel.Workbook workbook = excelApp.Workbooks.Open(@"C:\Users\John\Desktop\ExcelReport.xlsx", missing, readOnly, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
        //                excelApp.Application.Workbooks.Add(true);
        //                Excel._Workbook myBook = excelApp.Workbooks[2]; //获取excel程序的工作簿
        //                Excel._Worksheet mySheet = (Excel.Worksheet)myBook.ActiveSheet; //获取Workbook的活动工作表（最上层的工作表）。
        //                //mySheet.Cells[1, 1] = "=SUM(Sheet1!A6:A10)";
        //                foreach (var cell in CellsItems)
        //                {
        //                    mySheet.Cells[cell.Row, cell.Col] = cell.Content;
        //                }

        //                //Save As  Path
        //                mySheet.SaveAs(SaveAsFileFullPath, missing, missing, missing, missing, missing, missing, missing, missing, missing);
        //                //Excel.Worksheet sheet = workBook.Sheets[SheetName];

        //                //foreach (var cell in CellsItems)
        //                //{
        //                //    sheet.Cells[cell.Row, cell.Col] = cell.Content;
        //                //}
        //            }
        //            else
        //            {
        //                throw new Exception($"Sheet:[{SheetName}] not exist.");
        //            }
        //            workBook.Saved = true;
        //            workBook.SaveAs(SaveAsFileFullPath);
        //        }
        //    }
        //}
        public ExcelContent()
        {

        }
        public ExcelContent(string sheetName)
        {
            SheetName = sheetName;
        }
        public string SheetName { get; set; }
        public void WriteTo(Excel.Workbook workBook)
        {
            if (workBook.Sheets.ContainSheet(SheetName))
            {
                Excel.Worksheet sheet = workBook.Sheets[SheetName];
                    sheet.Cells[Cell.Row, Cell.Col] = Cell.Content;
            }
            else
            {
                throw new Exception($"[{SheetName}] not exist!");
            }
        }
    }
}
